#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
actress 视图模块
script name: views
Created on 2020/2/15 17:54 by PyCharm
@author: fujianjian
"""
from flask import render_template, flash, redirect, url_for, jsonify

from .. import db
from .forms import AddForm
from ..model.actress import Actress
from . import actress_blueprint


@actress_blueprint.route('/list', methods=['post', 'get'])
def actress_list():
    actress_page = Actress.query.filter().paginate(1, 10)
    return render_template('actress/list.html', actress_page=actress_page)


@actress_blueprint.route('/data', methods=['post', 'get'])
def actress_data():
    actress_page = Actress.query.filter().paginate(1, 10)
    resp = {
        'code':200,
        'msg': 'success',
        'data':{
            'total': actress_page.total,
            'total_page': actress_page.pages,
            'curr_page': actress_page.page,
            'page_size':actress_page.per_page,
            'contents': actress_page.items
        }
    }
    return jsonify(resp)


@actress_blueprint.route('/add', methods=['post', 'get'])
def add_actress():
    form = AddForm()
    if form.validate_on_submit():
        actress = Actress.query.filter(Actress.name == form.name.data).first()
        if actress is None:
            actress = Actress(name=form.name.data)
            db.session.add(actress)
            db.session.commit()
            flash('add actress is success')
        else:
            flash('the actress has exist')
        return redirect(url_for('.actress_list'))
    form.name.data = ''
    return render_template('actress/add.html', form=form)
